package cn.qiao.io;

import java.nio.file.*;
import static java.nio.file.StandardWatchEventKinds.*;
import java.io.IOException;

public class FileSystemWatcherExample {
    public static void main(String[] args) throws IOException {
        Path dir = Paths.get("H:\\Exercise\\test\\").toAbsolutePath();
        Path dir1 = Paths.get("H:\\Exercise\\").toAbsolutePath();
        Path dir3 = Paths.get("H:\\Exercise\\test\\demo\\").toAbsolutePath();
        Path dir4 = Paths.get("J:\\tmp\\").toAbsolutePath();
        System.out.println("Watching Directory: " + dir);

        try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
            dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            dir1.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            dir3.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            dir4.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);

            while (true) {
                WatchKey key = watcher.take();
                Watchable watchable = key.watchable();
                for (WatchEvent<?> event : key.pollEvents()) {
                    WatchEvent.Kind<?> kind = event.kind();
                    if (kind == OVERFLOW) {
                        continue;
                    }
                    // Context for directory entry event is the file name
                    @SuppressWarnings("unchecked")
                    WatchEvent<Path> ev = (WatchEvent<Path>) event;
                    Path fileName = ev.context();
                    // Print out event kind and file name
                    System.out.println(kind + " -> " + fileName);
                }

                // Reset the key
                boolean valid = key.reset();
                if (!valid) {
                    break;
                }
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }


//    public static void main(String[] args) throws IOException {
//        Path dir = Paths.get("H:\\Exercise\\test").toAbsolutePath();
//        Path dir1 = Paths.get("H:\\Exercise").toAbsolutePath();
//        System.out.println("Watching Directory: " + dir);
//        System.out.println("Watching Directory: " + dir1);
//
//        try (WatchService watcher = FileSystems.getDefault().newWatchService()) {
//            dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
//            dir1.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
//
//            while (true) {
//                WatchKey key = watcher.take();
//                Path watchedDir = (Path) key.watchable(); // Get the directory associated with this key
//
//                for (WatchEvent<?> event : key.pollEvents()) {
//                    WatchEvent.Kind<?> kind = event.kind();
//                    if (kind == OVERFLOW) {
//                        continue;
//                    }
//                    // Context for directory entry event is the file name
//                    @SuppressWarnings("unchecked")
//                    WatchEvent<Path> ev = (WatchEvent<Path>) event;
//                    Path fileName = ev.context();
//                    // Print out event kind, file name, and the directory it happened in
//                    System.out.println("[" + watchedDir + "] " + kind + " -> " + fileName);
//                }
//
//                // Reset the key
//                boolean valid = key.reset();
//                if (!valid) {
//                    break;
//                }
//            }
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//        }
//    }
}